package com.warehouse.module.business.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.DateUtils;
import com.warehouse.framework.redis.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

public class ValidationRulesUtils {

	public static String ORDER_NUM_KEY = "warehouse:order_num:{yearMonth}";
	
	//校验运单号是否正确
	//（前7位-第8位）/7 整除才正确  235-75254933
	public static Boolean checkTrackingNumber(String str) {
		boolean bool = false;
		
		if(StringUtils.isNotEmpty(str) && str.length()>11){
			String eightStr = str.substring(str.length()-8);
			
			int severInt = Integer.parseInt(eightStr.substring(0,7));
			int lastInt = Integer.parseInt(eightStr.substring(7));
			
			int remainder = (severInt -lastInt) % 7;
			
			if(remainder == 0){
				//如果余数为0,说明该运单号是正确的,此时返回true
				bool=true;
			}
		}
		
		return bool;
	}

	public static String createOrderId(){
		Date date = new Date();
		String year = DateUtils.format(date, "yy");
		String month = DateUtils.format(date, "MM");
		String key = StrUtil.format(ORDER_NUM_KEY, year+"-"+month);
		Long num = RedisUtil.incrAtomicValue(key);
		String number = StringUtils.leftPad(num+"", 3, "0");
		return "JR"+year+month+number;
	}
}
